<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/22
 * Time: 13:21
 */

namespace app\alliance\controller;

use app\service\BusinessService;
use app\service\IGeTuiService;
use app\service\jobService;
use app\service\MemberService;
use app\service\OcrService;
use app\service\ResourceService;
use app\service\TaskService;
use Redis\Redis;
use think\Db;
use think\db\Expression;

class Mine extends Common
{

    /**
     * @return array
     * @content 查询是否存在未确认未举报的交易厅信息
     */
    function tradingTobeConfirm()
    {
        # 查询是否存在未确认的交易厅信息
        $count = Db::table('trading_room')->where(array('member_code' => $this->code, 'status' => 3, 'report_status' => 1))->count();
        if ($count > 0) {
            $status = true;
        } else {
            $status = false;
        }
        return array('status' => $status);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content  交易厅 -- 交易之投诉审核
     */
    function tradingReport()
    {
        # 交易信息ID
        $tradingId = input('post.trading_id');
        # 接收投诉原因
        $reason = input('post.reason');
        # 接收投诉内容
        $complaint_context = input('post.complaint_context');
        # 接收投诉截图(英文逗号拼接字符串)
        $screenshot = json_decode(input("post.screenshot"), true);
        if (!empty($tradingId)) {
            # 查询交易信息
            $tradingInfo = Db::table('trading_room')->where(array('id' => $tradingId))->find();
            if ($tradingInfo['report_status'] != 1) {
                $msg = '已提交过审核,请耐心等待';
                if ($tradingInfo['report_status'] == 3) {
                    $msg = '审核已通过.不可重复提交审核';
                }
                if ($tradingInfo['report_status'] == 4) {
                    $msg = '审核已驳回.不可重复提交审核';
                }
                return array('status' => false, 'msg' => $msg);
            }
            # 投诉截图上传
            $_base_url = array();
            if (!empty($screenshot)) {
                foreach ($screenshot as $k => $v) {
                    # 上传截图
                    array_push($_base_url, ResourceService::uploadBase($v));
                }
            }
            $screenshot_info = implode(',', $_base_url);// 投诉图片逗号拼接
            # 修改交易信息中的投诉状态
            $res = Db::table('trading_room')->where(array('id' => $tradingId))->update(array('report_status' => 2));
            $redis = new Redis();
            $buyerInfo = $redis->hGetJson('memberInfo', $tradingInfo['buyer_code']);
            # 添加投诉信息
            Db::table('complaint')->insert(array(
                'buy_code' => $tradingInfo['buyer_code'],//被投诉人
                'buy_name' => $tradingInfo['buyer_name'],
                'buy_phone' => $buyerInfo['phone'],
                'industry_id' => $tradingId,
                'screenshot' => $screenshot_info,
                'sell_code' => $this->code,
                'sell_name' => $this->memberInfo['name'],
                'sell_phone' => $this->memberInfo['phone'],
                'status' => 1,
                'reason' => $reason,
                'complaint_context' => $complaint_context,
                'trad_audit_id' => $tradingId,
                'type_mark' => 2,  // 交易厅的投诉审核,
                'create_time' => date('Y-m-d H:i:s')
            ));
            return array('status' => true, 'msg' => '提交投诉审核成功');
        } else {
            return array('status' => false, 'msg' => '参数信息为空');
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 交易厅-交易确认完成
     */
    function trading_confirm()
    {
        # 交易信息id
        $tradingId = input('post.trading_id');
        if (!empty($tradingId)) {
            # 查询交易信息
            $tradingInfo = Db::table('trading_room')->where(array('id' => $tradingId))->find();
            if ($tradingInfo['status'] == 4) {
                return array('status' => false, 'msg' => '已经确认,不可重复操作');
            }
            # 修改状态
            Db::table('trading_room')->where(array('id' => $tradingId))->update(array(
                'status' => 4,
                'confirm_time' => date('Y-m-d H:i:s')
            ));
            # 卖家增加出售金币/积分记录 , 卖家增加金币/积分 , 购买金币/积分记录
            $redis = new Redis();
            $buyerInfo = $redis->hGetJson('memberInfo', $tradingInfo['buyer_code']);
            if ($tradingInfo['type'] == 1) {
                # 金币
                # 卖家
                Db::table('log_gold')->where(array('member_code' => $this->code, 'type' => 24, 'order_id' => $tradingId))->update(array('memberp_code' => $tradingInfo['buyer_code']));
                # 买家
                Db::table('member_property')->where(array('member_code' => $tradingInfo['buyer_code']))->setInc('gold', $tradingInfo['num']);
                MemberService::addLog_gold($tradingInfo['buyer_code'], $tradingInfo['num'], 25, $this->code);
                # 更新redis
                $buyerInfo['gold'] = priceFormat($buyerInfo['gold'] + $tradingInfo['num']);
            } else {
                # 积分
                # 卖家
                Db::table('log_integral')->where(array('member_code' => $this->code, 'order_id' => $tradingId, 'type' => 17))->update(array('memberp_code' => $tradingInfo['buyer_code']));
                # 买家
                Db::table('member_property')->where(array('member_code' => $tradingInfo['buyer_code']))->setInc('integral', $tradingInfo['num']);
                MemberService::addLog_integral($tradingInfo['buyer_code'], $tradingInfo['num'], 1, 18, $this->code);
                # 更新redis
                $buyerInfo['integral'] = priceFormat($buyerInfo['integral'] + $tradingInfo['num']);
            }
            $redis->hSetJson('memberInfo', $tradingInfo['buyer_code'], $buyerInfo);
            return array('status' => true);
        } else {
            return array('status' => false, 'msg' => '参数信息存在空值');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 交易厅-交易记录
     */
    function tradingRoom_log()
    {
        # 分页
        $page = input('post.page') ? input('post.page') : 0;
        $listRows = 10;
        $pageMark = $page * $listRows;
        # where条件
        $_where = "tr.member_code='" . $this->code . "' or tr.buyer_code='" . $this->code . "'";
        # 类型筛选  1 出售  2购进
        $type = input('post.type');
        if (!empty($type)) {
            if (in_array($type, [1, 3, 5, 7, 8])) {
                # 出售
                $_where = "tr.member_code = '" . $this->code . "'";
            } else {
                # 购进
                $_where = "tr.buyer_code = '" . $this->code . "'";
            }
            switch ($type) {
                case 1:
                case 4:
                    $_where .= " and tr.status=3 and tr.report_status=1";
                    break;
                case 2:
                case 3:
                    $_where .= " and tr.status=2 and tr.report_status=1";
                    break;
                case 5:
                case 6:
                    $_where .= " and tr.report_status=2";
                    break;
                case 7:
                    $_where .= " and tr.status=1 and tr.report_status=1";
                    break;
                case 8:
                case 9:
                    $_where .= " and tr.status=4 and tr.report_status=1";
                    break;
                case 10:
                case 11:
                    $_where .= " and tr.report_status=4";
                    break;
            }
        }
        # 排序条件
        $sortArr = new Expression("field(tr.status,3,2,1,4) asc,field(tr.report_status,2,0,3,4) asc");
//        $sortArr = array('field(tr.status,3,2,1,4)'=>'asc');
        # 时间排序(asc=正序 desc=>倒序)
        $timeSorting = input('post.time');
        if (!empty($timeSorting)) {
            $sortArr = array();
            $sortArr['tr.create_time'] = $timeSorting;
//            $sortArr = 'tr.create_time ' . $timeSorting;
        }
        # 价值排序(asc=正序 desc=>倒序)
        $priceSorting = input('post.price');
        if (!empty($priceSorting)) {
            $sortArr = array();
            $sortArr['tr.price'] = $priceSorting;
//            $sortArr = ' tr.price ' . $priceSorting;
        }
        # 查询交易记录
        $tradingInfo = Db::table('trading_room tr')
            ->field('tr.*,mc.wx_number,mc.ali_number,mc.wx_img,mc.wx_img')
            ->join('member_collection mc', 'mc.member_code=tr.member_code', 'left')
            ->where($_where)
            ->order($sortArr)
            ->limit($pageMark, $listRows)
            ->select();
        if (!empty($tradingInfo)) {
            foreach ($tradingInfo as $k => $v) {
                $tradingInfo[$k]['create_time'] = date('Y-m-d H:i', strtotime($v['create_time']));
                $tradingInfo[$k]['order_time'] = date('Y-m-d H:i', strtotime($v['order_time']));
                $tradingInfo[$k]['order_over_time'] = date('Y-m-d H:i', strtotime('+1 hour', strtotime($v['order_time'])));
                $tradingInfo[$k]['pay_time'] = date('Y-m-d H:i', strtotime($v['pay_time']));
                $tradingInfo[$k]['confirm_time'] = date('Y-m-d H:i', strtotime($v['confirm_time']));
                $tradingInfo[$k]['confirm_over_time'] = date('Y-m-d H:i', strtotime('+2 hours', strtotime($v['pay_time'])));
                if ($v['member_code'] == $this->code) {
                    $tradingInfo[$k]['self'] = 1;
                    switch ($v['status']) {
                        case 1:
                            $tradingInfo[$k]['statusInfo'] = '出售中';
                            break;
                        case 2:
                            $tradingInfo[$k]['statusInfo'] = '已出售(待支付)';
                            break;
                        case 3:
                            $tradingInfo[$k]['statusInfo'] = '已出售(待确认)';
                            break;
                        case 4:
                            $tradingInfo[$k]['statusInfo'] = '已出售';
                    }
                    switch ($v['report_status']) {
                        case 2:
                            $tradingInfo[$k]['statusInfo'] = '投诉/举报-审核中';
                            break;
                        case 3:
                            $tradingInfo[$k]['statusInfo'] = '投诉/举报-审核通过';
                            break;
                        case 4:
                            $tradingInfo[$k]['statusInfo'] = '投诉/举报-审核驳回';
                    }
                } else {
                    $tradingInfo[$k]['self'] = 2;
                    switch ($v['status']) {
                        case 2:
                            $tradingInfo[$k]['statusInfo'] = '已收购(待支付)';
                            break;
                        case 3:
                            $tradingInfo[$k]['statusInfo'] = '已收购(待确认)';
                            break;
                        case 4:
                            $tradingInfo[$k]['statusInfo'] = '已收购(已完成)';
                    }
                    switch ($v['report_status']) {
                        case 2:
                            $tradingInfo[$k]['statusInfo'] = '被投诉/举报-审核中';
                            break;
                        case 3:
                            $tradingInfo[$k]['statusInfo'] = '被投诉/举报-审核通过';
                            break;
                        case 4:
                            $tradingInfo[$k]['statusInfo'] = '被投诉/举报-审核驳回';
                    }
                }
                if($v['status']==1){
                    $tradingInfo[$k]['color'] = 'green';
                }elseif ($v['status']==2){
                    $tradingInfo[$k]['color'] = 'red';
                }elseif ($v['status']==3){
                    $tradingInfo[$k]['color'] = 'orange';
                }else{
                    $tradingInfo[$k]['color'] = 'grey';
                }
            }
        }
        return array('status' => true, 'msg' => '查询成功', 'info' => $tradingInfo);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 交易厅---确认支付
     */
    function tradingPay()
    {
        # 支付方式  1微信  2支付宝
        $pay_type = input('post.pay_type');
        # 支付凭证
        $trading_proof = input('post.trading_proof');
        # 交易信息id
        $trading_id = input('post.trading_id');
        if (!empty($pay_type) and !empty($trading_proof) and !empty($trading_id)) {
            # 判断交易信息状态
            $tradingInfo = Db::table('trading_room')->field("buyer_code,status")->where(array('id' => $trading_id))->find();
            if (($tradingInfo['status'] == 2 and $tradingInfo['buyer_code'] != $this->code) or $tradingInfo['status'] == 3 or $tradingInfo['status'] == 4) {
                return array('status' => false, 'msg' => '该交易已被别人抢先了');
            }
            # 上传交易凭证
            $proof = ResourceService::uploadBase($trading_proof);
            if (!$proof) {
                return array('status' => false, 'msg' => '交易凭证上传失败');
            }
            # 支付成功 , 更改状态
            Db::table('trading_room')->where(array('id' => $trading_id))->update(array(
                'status' => 3,
                'pay_time' => date('Y-m-d H:i:s'),
                'trading_proof' => $proof
            ));
            # 给卖家发送消息,提示确认
            #
            #
            return array('status' => true);
        } else {
            return array('status' => false, 'msg' => '参数信息存在空值');
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 交易厅--确认购买
     */
    function confirmPurchase()
    {
        # 交易厅信息ID
        $tradingRoom_id = input('post.trading_id');
        if (!empty($tradingRoom_id)) {
            # 先判断 该交易信息的状态
            $tradingStatus = Db::table('trading_room')->field('status')->where(array('id' => $tradingRoom_id))->find()['status'];
            if ($tradingStatus != 1) {
                return array('status' => false, 'msg' => '该交易被别人抢先了');
            }
            # 确认购买,更改状态
            Db::table('trading_room')->where(array('id' => $tradingRoom_id))->update(array(
                'status' => 2,
                'buyer_code' => $this->code,
                'buyer_name' => $this->memberInfo['name'],
                'buyer_head' => $this->memberInfo['head_portrait'],
                'order_time' => date('Y-m-d H:i:s')
            ));
            return array('status' => true);
        } else {
            return array('status' => false, 'msg' => '参数为空');
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @content 发布交易信息
     */
    function release()
    {
//        # 判断今天是否发不过交易信息
//        $logInfo = Db::table('trading_room')->where(array('member_code' => $this->code))->order(array('id' => 'desc'))->find();
//        if (!empty($logInfo)) {
//            if (date('Y-m-d', strtotime($logInfo['create_time'])) == date('Y-m-d')) {
//                return array('status' => false, 'msg' => '一天只能发布一次');
//            }
//        }
        if ($this->memberInfo['level'] < 3) {
            return array('status' => false, 'msg' => '等级不足，三级玩家才可发布');
        }
        # 判断是否完善收款信息
        if (empty($this->memberInfo['wx_number']) and empty($this->memberInfo['ali_number'])) {
            return array('status' => false, 'msg' => '请先完善收款信息');
        }
        # 出售类型 (1->金币  2->积分)
        $type = input('post.type');
        # 出售数量
        $num = input('post.num');
        # 出售金额
        $price = input('post.price');
        # 计算手续费
        $serviceCharge = $num * 0.1;
        if ($serviceCharge < 10) {
            $serviceCharge = 10;
        }
        if ($serviceCharge > 100) {
            $serviceCharge = 100;
        }
        if ($type == 1) {
            # 判断是否到6级可以出售绑定金币
            if($this->memberInfo['level']<6){
                # 小于6级查看金币是否足够
                # 判断金币是否足够
                if ($this->memberInfo['gold'] < ($num + $serviceCharge)) {
                    return array('status' => false, 'msg' => '金币不足');
                }
            }else{
                # 大于6级查看绑定金币和金币
                if ($this->memberInfo['gold']+$this->memberInfo['bind_gold'] < ($num + $serviceCharge)) {
                    return array('status' => false, 'msg' => '金币不足');
                }
            }

            if (intval($price / $num) < 0.8) {
                return array('status' => false, 'msg' => '每个金币价格不能小于0.8元');
            }
        } else {
            # 判断积分是否足够
            if ($this->memberInfo['integral'] < ($num + $serviceCharge)) {
                return array('status' => false, 'msg' => '积分不足');
            }
        }

        if (!empty($num) and !empty($type)) {
            # 发布交易信息
            $tradingId = Db::table('trading_room')->insert(array(
                'type' => $type,
                'num' => $num,
                'price' => $price,
                'member_code' => $this->code,
                'member_name' => $this->memberInfo['name'],
                'head_portrait' => $this->memberInfo['head_portrait'],
                'create_time' => date('Y-m-d H:i:s'),
                'service_charge' => $serviceCharge
            ));
            # 扣除金币/积分
            if ($type == 1) {
                if($this->memberInfo['level']<6){
                    # 扣除金币
                    Db::table('member_property')->where(array('member_code' => $this->code))->setDec('gold', ($num + $serviceCharge));
                }else{
                    if($this->memberInfo['bind_gold']>=($num + $serviceCharge)){
                        # 扣除绑定金币
                        Db::table('member_property')->where(array('member_code' => $this->code))->setDec('bind_gold', ($num + $serviceCharge));
                    }else{
                        Db::table('member_property')->where(array('member_code' => $this->code))->setDec('bind_gold', $this->memberInfo['bind_gold']);
                        Db::table('member_property')->where(array('member_code' => $this->code))->setDec('gold', ($num + $serviceCharge-$this->memberInfo['bind_gold']));
                        $this->memberInfo['gold']-=$num + $serviceCharge-$this->memberInfo['bind_gold'];
                        $this->memberInfo['bind_gold']=0;
                    }
                }

                # 添加记录(手续费)
                MemberService::addLog_gold($this->code, $serviceCharge, 23);
                # 卖家
                MemberService::addLog_gold($this->code, $num, 24, '', 0, '', $tradingId);
                # 更新redis
                //$this->memberInfo['gold'] = priceFormat($this->memberInfo['gold'] - ($num + $serviceCharge));
            } else {
                # 扣除积分
                Db::table('member_property')->where(array('member_code' => $this->code))->setDec('integral', ($num + $serviceCharge));
                # 添加记录
                MemberService::addLog_integral($this->code, $serviceCharge, 2, 16);
                # 卖家
                MemberService::addLog_integral($this->code, $num, 2, 17, $tradingId);
                # 更新redis
                $this->memberInfo['integral'] = priceFormat($this->memberInfo['integral'] - ($num + $serviceCharge));
            }
            # 更新redis
            $redis = new Redis();
            $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
            return array('status' => true);
        } else {
            return array('status' => false, 'msg' => '参数存在空值');
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 交易厅
     */
    function trading_room()
    {
        # 先删除超过24小时的信息
//        Db::table('trading_room')->where("date(create_time) = '" . date('Y-m-d',strtotime('-1 day'))."'")->delete();
        # 查询列表
        $listRows = 20;
        $pageMark = input('post.pageMark');
        if (empty($pageMark)) {
            $pageMark = 0;
        }
        $page = $pageMark * $listRows;
        $info = Db::table('trading_room tr')
            ->field('tr.id,tr.type,tr.num,tr.price,tr.member_code,tr.member_name,tr.head_portrait,tr.create_time,
            mc.wx_number,mc.ali_number,mc.wx_img,mc.wx_img')
            ->join('member_collection mc', 'mc.member_code=tr.member_code', 'left')
            ->where(array('tr.status' => 1))
            ->order(array('tr.id' => 'desc'))
            ->limit($page, $listRows)
            ->select();
        # 等待操作的数量
        $confirmCount = Db::table('trading_room')
            ->where("(member_code='" . $this->code . "' and status=3 and report_status=1) or (buyer_code='" . $this->code . "' and status=2)")
            ->count();
        return array('status' => true, 'msg' => '查询成功', 'info' => $info, 'confirmCount' => $confirmCount);
    }


    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @content 修改昵称
     */
    function changeNickname()
    {
        $name = input('post.name');
        if (!empty($name)) {
            # 修改昵称
            Db::table('member')->where(array('code' => $this->code))->update(array('name' => $name));
            $this->memberInfo['name'] = $name;
            $redis = new Redis();
            $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
            # 修改好友列表里面的昵称
            Db::table('friends')
                ->where(array('friends_code' => $this->code, 'status' => 2))
                ->update(array('nickname' => $name));
            # 修改聊天记录的姓名
            $log = Db::connect("db_config2")->table("chat")
                ->where(array("relevance" => $this->code))->select();
            if (!empty($log)) {
                foreach ($log as $k => $v) {
                    $content = json_decode($v['content'], true);
                    $content['friendName'] = $name;
                    Db::connect("db_config2")->table("chat")
                        ->where(array('id' => $v['id']))
                        ->update(array("content" => json_encode($content, JSON_UNESCAPED_UNICODE), 'receipt_name' => $name));
                }
            }
            # 修改交易厅里面的姓名(出售人/购买人)
            Db::table('trading_room')->where(array('member_code' => $this->code))->update(array('member_name' => $name));
            Db::table('trading_room')->where(array('buyer_name' => $this->code))->update(array('buyer_name' => $name));
            return array('status' => true, 'msg' => '修改成功');
        } else {
            return array('status' => false, 'msg' => '昵称信息为空');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 金币充值
     */
    function goldRecharge()
    {
        $gold = input('post.gold');
        $money = $gold * 1.2;
        $payType = input('post.payType');
        if ($gold > 0 and !empty($payType)) {
            $out_trade_no = time() . $this->code;
            # 查询是否有满送
            $give_gold = Db::table('full_delivery')->where("amount <= " . $gold)->order("amount desc")->limit(1)->find()['give_num'];
            if (empty($give_gold)) {
                $give_gold = 0;
            }
            # 充值记录
            $rechargeLog = array(
                'member_code' => $this->code,
                'recharge_amount' => $money,
                'out_trade_no' => $out_trade_no,
                'recharge_gold' => $gold,
                'give_gold' => $give_gold,
                'create_time' => date('Y-m-d H:i:s')
            );
            if ($payType == 1) {
                # 支付宝
                # 充值方式
                $rechargeLog['pay_type'] = 1;
            } else {
                # 微信
                # 充值方式
                $rechargeLog['pay_type'] = 2;
            }
            # 添加充值记录
            Db::table('log_recharge')->insert($rechargeLog);
            # 完成任务-->金币充值
            TaskService::finishTask($this->code, 19);
            return array('status' => true, 'msg' => '', 'orderInfo' => $rechargeLog);
        } else {
            return array('status' => false, 'msg' => '参数不正确');
        }
    }

    /**
     * @return array
     * @content 统一下单
     */
    function unifiedOrder()
    {
        # 订单编号
        $out_trade_no = input('post.out_trade_no');
        # 支付金额
        $total_amount = input('post.total_amount');
        # 支付方式
        $payType = input('post.payType');
//        $total_amount = 0.01;
        if (!empty($out_trade_no) and !empty($total_amount) and !empty($payType)) {
            if ($payType == 1) {
                # 支付宝
                $orderInfo = [
                    'out_trade_no' => $out_trade_no,
                    'total_amount' => $total_amount,
                    'subject' => '金币充值',
                ];
                # 支付-统一下单
                $payMent = new Payment($orderInfo);
                $res = $payMent->aliPay();
            } else {
                # 微信
                $orderInfo = [
                    'out_trade_no' => $out_trade_no,
                    'body' => '金币充值',
                    'total_fee' => $total_amount * 100,
                ];
                # 支付--统一下单
                $payMent = new Payment($orderInfo);
                $res = $payMent->wxPay();
            }
            return $res;
        } else {
            return array('status' => false, 'msg' => '参数不正确(存在空值)');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 满送
     */
    function full_delivery()
    {
        # 查询满送活动
        $info = Db::table('full_delivery')->order(array('amount' => 'asc'))->select();
        return array('status' => true, 'msg' => '查询成功', 'info' => $info);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @content 玩家退出
     */
    function cancellation()
    {
        Db::table('member')->where(array('code' => $this->code))->update(array('type' => 4));
        $redis = new Redis();
        $redis->hDel('memberInfo', $this->code);
        return array('status' => true);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 领取体验行业
     */
    function receiveBusiness()
    {
        $res = MemberService::receiveBusiness($this->code);
        return $res;
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 立即升级
     */
    function upgradeNow()
    {
        $redis = new Redis();
        # 查询用户下一个等级信息
        $nextLevel = $this->memberInfo['level'] + 1;
        if ($nextLevel > 12) {
            return array('status' => false, 'msg' => '已经是最高等级');
        } else {
            $nextLevelInfo = $redis->hGetJson('memberLevelInfo', strval($nextLevel));
            if (empty($nextLevelInfo)) {
                $nextLevelInfo = Db::table('member_level')->where(array('level' => $nextLevel))->find();
                $redis->hSetJson('memberLevelInfo', strval($nextLevel), $nextLevelInfo);
            }
        }
        if ($this->memberInfo['integral'] < $nextLevelInfo['integral']) {
            # 积分不足
            return array('status' => false, 'msg' => '积分不足');
        }
        # 判断升级条件是否都完成
        $canUpgrade = $redis->hGet('MemberCanUpgrade', $this->code);
        if ($canUpgrade != 'YES') {
            # 未完成升级条件
            return array('status' => false, 'msg' => '未完成升级条件');
        }
        # 进行升级, 修改等级
        Db::table('member')->where(array('code' => $this->code))->update(array('level' => $nextLevel));
        $this->memberInfo['level'] = $nextLevel;
        $this->memberInfo['title'] = $nextLevelInfo['title'];
        # 扣积分
        Db::table('member_property')->where(array('member_code' => $this->code, 'is_fake' => 1))->setDec('integral', $nextLevelInfo['integral']);
        $this->memberInfo['integral'] = priceFormat($this->memberInfo['integral'] - $nextLevelInfo['integral']);
        # 积分记录
        MemberService::addLog_integral($this->code, $nextLevelInfo['integral'], 2, 1);
        # 开格子
        Db::table('member_business_grid')->where("member_code = '" . $this->code . "' and grid = " . (intval($nextLevelInfo['level']) + 1))->update(array('status' => 1));
        #更新redis(用户信息,是否可以升级)
        $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
        $redis->hSet('MemberCanUpgrade', $this->code, 'NO');
        # 修改栏位缓存
        $list = $redis->hGetJson("member_business", $this->code);
        if (!empty($list)) {
            $list[$nextLevel]['open_status'] = 1;
            $redis->hSetJson("member_business", $this->code, $list);
        }
        # 完成任务=>达成级别
        TaskService::finishTask($this->code, 2);
        if ($nextLevel == 3) {
            # 达到第三极的时候添加收益
            if (!empty($this->memberInfo['p_code'])) {
                # 添加收益
                MemberService::recommendedRevenue($this->memberInfo['p_code'], $this->code);
                # 完成任务=>推荐玩家
                TaskService::finishTask($this->memberInfo['p_code'], 1);
                # 完成任务=>团队人数
                TaskService::finishTask($this->memberInfo['p_code'], 5);
            }
        }
        return array('status' => true, 'msg' => '升级成功');
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 玩家生意列表
     */
    function memberBusiness()
    {
        # 查询玩家生意
        $business = new BusinessService();
        $businessInfo = $business->getsMemberBusiness();
        $wareHouseList = $business->wareHouseList();
        $info = array_merge($businessInfo, $wareHouseList);
        return array('status' => true, 'msg' => '查询成功', 'info' => $info);
        //开启 可购买->超过24小时未购买的为不活跃
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 等级介绍
     */
    function gradeIntroduce()
    {
        $redis = new Redis();
        # 查询用户当前等级信息
        $currentLevelInfo = $redis->hGetJson('memberLevelInfo', strval($this->memberInfo['level']));
        if (empty($currentLevelInfo) or empty($currentLevelInfo['direct'])) {
            $currentLevelInfo = Db::table('member_level')->where(array('level' => $this->memberInfo['level']))->find();
            $deduct = jobService::getLevelDeduct($this->memberInfo['level']);
            $currentLevelInfo['direct'] = $deduct[0];
            $currentLevelInfo['indirect'] = $deduct[1];
            $redis->hSetJson('memberLevelInfo', strval($this->memberInfo['level']), $currentLevelInfo);

        }
        # 查询用户下一个等级信息
        $nextLevel = $this->memberInfo['level'] + 1;
        if ($nextLevel > 12) {
            $nextLevelInfo = array();
            $nextLevelMsg = "已经是最高等级";
        } else {
            $nextLevelInfo = $redis->hGetJson('memberLevelInfo', strval($nextLevel));
            if (empty($nextLevelInfo) or empty($nextLevelInfo['direct'])) {
                $nextLevelInfo = Db::table('member_level')->where(array('level' => $nextLevel))->find();
                $deduct = jobService::getLevelDeduct($nextLevel);
                $nextLevelInfo['direct'] = $deduct[0];
                $nextLevelInfo['indirect'] = $deduct[1];
                $redis->hSetJson('memberLevelInfo', strval($nextLevel), $nextLevelInfo);
            }
            $nextLevelMsg = $nextLevelInfo['title'];
        }
        # 是否可以升级
        $canUpgrade = 'YES';
        # 升级标准(需要有多少个等级玩家)
        $gradeNumberLimit = array();
        if ($nextLevelInfo['is_limits'] == 2) {
            $gradeNumberLimit = Db::table('industry_level_limits ill')
                ->field('member_num,member_level,title,
                if(member_num > (select count(id) from member where member.status=5 and member.fictitious != 1 and  level=ill.member_level and p_code = "' . $this->code . '"),0,1) isCompleted')
                ->leftJoin('member_level ml', 'ml.level=ill.member_level')
                ->where(array('ill.level_id' => $nextLevelInfo['id']))
                ->order("member_level asc")
                ->select();
            if (!empty($gradeNumberLimit)) {
                if (in_array(0, array_column($gradeNumberLimit, 'isCompleted'))) {
                    $canUpgrade = 'NO';
                }
            }
        }
        # 判断升级条件是否满足(默认满足)(交易次数 , 直推人数 , 团队人数 , 消费金额限制,金币充值次数限制)
        $upgradeStandard = array('trade_num_complete' => 1, 'share_num_complete' => 1, 'team_people_complete' => 1, 'consumption_amount_complete' => 1, 'recharge_times_complete' => 1);
        if ($nextLevelInfo['trade_num'] > 0) {
            # 查询交易次数
            $tradNum = Db::table('log_trad')->where("seller_code = '" . $this->code . "' or buyer_code = '" . $this->code . "'")->count();
            if ($tradNum < $nextLevelInfo['trade_num']) {
                $upgradeStandard['trade_num_complete'] = 0;
                $canUpgrade = 'NO';
            }
        }
        if ($nextLevelInfo['share_num'] > 0) {
            # 查询直推人数
            $shareNum = Db::table('member')->where(array('p_code' => $this->code, 'fictitious' => 0, 'status' => 5))->count();
            if ($shareNum < $nextLevelInfo['share_num']) {
                $upgradeStandard['share_num_complete'] = 0;
                $canUpgrade = 'NO';
            }
        }
        if ($nextLevelInfo['team_people'] > 0) {
            # 查询团队人数
            $teamNum = Db::table('member')->field('code')
                ->where("code != '" . $this->code . "'")
                ->where("(code in (
            (select code from member where status=5 and fictitious=0 and p_code in(select code from member where p_code = '" . $this->code . "'))))")->count();
            if ($teamNum < $nextLevelInfo['team_people']) {
                $upgradeStandard['team_people_complete'] = 0;
                $canUpgrade = 'NO';
            }
        }
        if ($nextLevelInfo['consumption_amount'] > 0) {
            # 消费金额限制
            $consumptionAmount = Db::table('log_integral')->where(array('member_code' => $this->code, 'type_branch' => 7))->sum('total_money');
            if ($consumptionAmount < $nextLevelInfo['consumption_amount']) {
                $upgradeStandard['consumption_amount_complete'] = 0;
                $canUpgrade = 'NO';
            }
            $upgradeStandard['diff'] = $nextLevelInfo['consumption_amount'] - $consumptionAmount;

        }
        if ($nextLevelInfo['recharge_times'] > 0) {
            # 金币充值次数限制
            $rechargeTimes = Db::table('log_recharge')->where(array('member_code' => $this->code, 'status' => 2))->count();
            if ($rechargeTimes < $nextLevelInfo['recharge_times']) {
                $upgradeStandard['recharge_times_complete'] = 0;
                $canUpgrade = 'NO';
            }
        }
        $redis->hSet('MemberCanUpgrade', $this->code, $canUpgrade);
        if ($canUpgrade == 'YES') {
            # 获取当前用户可开启的栏位
            $res = Db::table("member_business_grid")->where(array("member_code" => $this->code, "grid" => $nextLevel + 1, "status" => 0))->update(array("status" => 2));
            if ($res) {
                # 修改栏位缓存
                $list = $redis->hGetJson("member_business", $this->code);
                if (!empty($list)) {
                    $list[$nextLevel]['open_status'] = 2;
                    $redis->hSetJson("member_business", $this->code, $list);
                }
            }
        }
        return array('status' => true, 'msg' => '查询成功', 'currentLevelInfo' => $currentLevelInfo,
            'nextLevelInfo' => $nextLevelInfo, 'nextLevelMsg' => $nextLevelMsg, 'gradeNumberLimit' => $gradeNumberLimit,
            'upgradeStandard' => $upgradeStandard, 'canUpgrade' => $canUpgrade);

    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 每日答题 - 领取奖励
     */
    function answerReward()
    {
        $res = MemberService::giveReward($this->memberInfo);
        # 今日已经答题redis
        $redis = new Redis();
        $time = strtotime(date('Y-m-d 23:59:59')) - time();
        $redis->hSet('hasQuestionLog', $this->code, 'YES', $time);
        # 完成任务=>每日答题
        TaskService::finishTask($this->code, 17);
        return $res;
    }

    /**
     * @return array
     * @content 添加答题记录
     */
    function addLogAnswers()
    {
        #  添加答题记录
        # 接收答题情况
        $info = input('post.');
        if (!empty($info) and is_array($info)) {
            $logArr = array();
            $wrong = array();
            foreach ($info as $k => $v) {
                if ($v['right_status'] == 2) {
                    array_push($wrong, $v['id']);
                }
                array_push($logArr, array(
                    'question_id' => $v['id'],
                    'member_code' => $this->code,
                    'right_status' => $v['right_status'],//1 正确 2错误
                    'create_time' => date("Y-m-d H:i:s")
                ));
            }
            if (!empty($logArr)) {
                # 错题
                $redis = new Redis();
                $time = strtotime(date('Y-m-d 23:59:59')) - time();
                $redis->hSetJson('wrongList', $this->code, $wrong, $time);
                $res = Db::table('log_member_question')->insertAll($logArr);
                if ($res) {
                    return array('status' => true, 'msg' => '成功');
                } else {
                    return array('status' => false, 'msg' => '失败');
                }
            } else {
                return array('status' => false, 'msg' => '添加失败,添加内容为空');
            }
        } else {
            return array('status' => false, 'msg' => '数据为空/数据类型错误');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 查询今日答题列表(判断今日是否已经答过题)
     */
    function answerList()
    {

        # 判断今天是否进行答题
        $redis = new Redis();
//        $redis->hDel('hasQuestionLog',$this->code);
        $isLog = $redis->hGet('hasQuestionLog', $this->code);
        if (empty($isLog)) {
            # 判断今天是否已经答过题
            $isLog = Db::table('log_member_question')->where(array('member_code' => $this->code, 'right_status' => 1))->where("date(create_time) = '" . date('Y-m-d') . "'")->count();
            if ($isLog == 3) {
                # 答对三题
                $time = strtotime(date('Y-m-d 23:59:59')) - time();
                $redis->hSet('hasQuestionLog', $this->code, 'YES', $time);
                return array('status' => false, 'msg' => '今日已经答过题了');
            }
            # 判断是否存在错题列表
            $wrong = $redis->hGetJson('wrongList', $this->code);
            if (!empty($wrong)) {
                $list = Db::table('question_bank qb')
                    ->field('qb.id,qb.title,qb.type')
                    ->where("id in (" . implode(',', $wrong) . ")")
                    ->limit(3)
                    ->select();
            } else {
                # 随机查询3个问题
//            $list = Db::query("select id,title,type from question_bank where status = 1 order by rand() limit 3");
                $list = Db::table('question_bank qb')
                    ->field('qb.id,qb.title,qb.type')
                    ->where("qb.status=1 and
              qb.id >= ((SELECT MAX(id) FROM question_bank where status=1)-(SELECT MIN(id) FROM question_bank where status=1)) * RAND()
              + (SELECT MIN(id) FROM question_bank where status=1)")
                    ->limit(3)
                    ->select();
            }
            if (!empty($list)) {
                # 根据问题查询答案
                foreach ($list as $k => $v) {
                    $answer = Db::table('question_answer')
                        ->field('id,context,right_answer,false choose,if(right_answer=1,true,false) correct')
                        ->where(array('question_id' => $v['id']))
                        ->select();
                    if (!empty($answer)) {
                        $list[$k]['answers'] = $answer;
                    } else {
                        unset($list[$k]);
                    }
                }
                return array('status' => true, 'msg' => '查询成功', 'info' => $list);
            } else {
                return array('status' => false, 'msg' => '问题列表为空');
            }
        } else {
            # 今天已经答过题了
            return array('status' => false, 'msg' => '今日已经答过题了');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 等级图鉴
     */
    function gradeMap()
    {
        $redis = new Redis();
        $info = $redis->hGetJson('industryInfo', 'list');
        if (empty($info)) {
            # redis为空 , 数据库查询行业等级图鉴(不包含体验等级和最高合成行业)
            $info = Db::table('industry i')
                ->field('i.id,title,logo,min_worth,max_worth,i.desc,detail,
                (select min(day_income) from industry_cycleincome where industry_id=i.id) min_ratio,
                (select max(day_income) from industry_cycleincome where industry_id=i.id) max_ratio
                ')
                ->where("i.is_del=2 and i.category=1 and i.status=1")
                ->order('i.level asc')
                ->select();
            # 存入redis
            $redis->hSetJson('industryInfo', 'list', $info);
        }
        return array('status' => true, 'info' => $info, 'msg' => '查询成功');
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 等级图鉴详情
     */
    function gradeMapDetail()
    {
        $id = input('post.id');
        if (!empty($id)) {
            $redis = new Redis();
            $info = $redis->hGetJson('industryDetail', strval($id));
            if (empty($info['info'])) {
                $detail = Db::table('industry i')
                    ->field('i.id,i.title,logo,min_worth,max_worth,i.desc,detail,ml.title levelTitle,
                (select min(day_income) from industry_cycleincome where industry_id=i.id) min_ratio,
                (select max(day_income) from industry_cycleincome where industry_id=i.id) max_ratio,
                (select min(cycle_period) from industry_cycleincome where industry_id=i.id) min_period,
                (select max(cycle_period) from industry_cycleincome where industry_id=i.id) max_period
                ')
                    ->leftJoin('member_level ml', 'ml.level=i.level')
                    ->where(array('i.id' => $id))
                    ->find();
                $cycleIncome = Db::table('industry_cycleincome')->where(array('industry_id' => $id))->select();
                $detail['detail'] = ResourceService::ContentStaticReplace($detail['detail']);
                $info = array('info' => $detail, 'cycleIncome' => $cycleIncome);
                $redis->hSetJson('industryDetail', strval($id), $info);
            }
            return array('status' => true, 'msg' => '查询成功', 'info' => $info['info']);
        } else {
            return array('status' => false, 'msg' => '参数为空');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 玩家排行
     */
    function playerRanking()
    {
        $listRows = 10;
        # 查询玩家排行
        $type = input('post.type');
        $page = input('post.page') * $listRows;
        if (empty($page)) {
            $page = 0;
        }
        if ($type == 'income') {
            # 收益排行
            $info = Db::table('member_property mp')
                ->field('if(is_fake=1,m.name,mp.nickname) name,if(is_fake=1,m.head_portrait,mp.thumb) imgUrl,convert(mp.income,decimal(10,2)) num')
                ->leftJoin('member m', 'm.code=mp.member_code')
                ->where('income >0')
                ->order('income desc')
                ->limit($page, $listRows)
                ->select();
        } elseif ($type == 'integral') {
            # 积分排行
            $info = Db::table('member_property mp')
                ->field('if(is_fake=1,m.name,mp.nickname) name,if(is_fake=1,m.head_portrait,mp.thumb) imgUrl,convert(mp.integral,decimal(10,2)) num')
                ->leftJoin('member m', 'm.code=mp.member_code')
                ->where("integral > 0")
                ->order('integral desc')
                ->limit($page, $listRows)
                ->select();
        } elseif ($type == 'team') {
            # 团队排行
            $info = Db::table('team')
                ->field('title name,team_num,convert(team_earnings,decimal(10,2)) num')
                ->where('team_earnings >0')
                ->order('team_earnings desc')
                ->limit($page, $listRows)
                ->select();
        } else {
            # 类型错误
            return array('status' => false, 'msg' => '排行类型错误');
        }
        if (!empty($info)) {
            foreach ($info as $k => $v) {
                $info[$k]['imgUrl'] = imgUrl($v['imgUrl']);
                if (empty($info[$k]['imgUrl'])) {
                    $info[$k]['imgUrl'] = '/static/img/5.jpg';
                }
            }
        }
        return array('status' => true, 'info' => $info, 'msg' => '查询成功');
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 帮助中心
     */
    function helpCenter()
    {
        $redis = new Redis();
        $info = $redis->hGetJson('HelpCenter', 'List');
        if (empty($info)) {
            $info = Db::table('qtype')->order('id desc')->select();
            if (!empty($info)) {
                foreach ($info as $k => $v) {
                    $info[$k]['icon'] = imgUrl($v['qtype_imgurl']);
                    $list = Db::table('qanswer')->where(array('qtype_id' => $v['id']))->select();
                    if (!empty($list)) {
                        foreach ($list as $lk => $lv) {
                            $list[$lk]['answer'] = ResourceService::ContentStaticReplace($lv['answer']);
                        }
                        $info[$k]['list'] = $list;
                    } else {
                        unset($info[$k]);
                    }
                }
                $redis->hSetJson('HelpCenter', 'List', $info);
            }
        }
        return array('status' => true, 'msg' => '查询成功', 'info' => $info);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @content 上传头像
     */
    function uploadAvatar()
    {
        $info = input('post.img');
        if (!empty($info)) {
            # 微信/支付宝  收款码
            $imgUrl = ResourceService::uploadBase($info);
            if ($imgUrl) {
                $_imgUrl = imgUrl($imgUrl);
                $redis = new Redis();
                # 修改头像
                Db::table('member')->where(array('code' => $this->code))->update(array('head_portrait' => $imgUrl));
                $del_img = $_delSrc = ROOT_PATH . DS . 'upload' . DS . $this->memberInfo['head_portrait'];
                $this->memberInfo['head_portrait'] = imgUrl($imgUrl);
                $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
                # 删除原有图片
                ResourceService::delFile($del_img);
                # 修改好友列表里面的头像信息
                Db::table('friends')->where(array('friends_code' => $this->code))->update(array('thumb' => $imgUrl));
                # 修改历史记录里面的头像信息
                $log = Db::connect("db_config2")->table("chat")
                    ->where(array("relevance" => $this->code))->select();
                if (!empty($log)) {
                    foreach ($log as $k => $v) {
                        $content = json_decode($v['content'], true);
                        $content['friendHead'] = $_imgUrl;
                        Db::connect("db_config2")->table("chat")
                            ->where(array("id" => $v['id']))
                            ->update(array("content" => json_encode($content)));
                    }
                }
                # 修改交易厅里面的头像信息(出售人/购买人)
                Db::table('trading_room')->where(array('member_code' => $this->code))->update(array('head_portrait' => imgUrl($_imgUrl)));
                Db::table('trading_room')->where(array('buyer_code' => $this->code))->update(array('buyer_head' => imgUrl($_imgUrl)));
                return array('status' => true, 'msg' => '上传成功', 'imgUrl' => $_imgUrl);
            } else {
                return array('status' => false, 'msg' => '上传失败');
            }
        } else {
            # 参数为空
            return array('status' => false, 'msg' => '上传的头像图片为空');
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @content 修改密码(登录密码/操作密码)
     */
    function changePwd()
    {
        # where 条件
        $_where = "code = '" . $this->code . "'";
        # 接收修改信息
        $info = input('post.');
        # 修改信息
        $updateInfo = array();
        $redis = new Redis();
        # 判断原密码是否相同
        if ($info['mark'] == 'login') {
            # 修改登录密码
            if ($this->memberInfo['login_pwd'] != md5($info['old_pwd'])) {
                return array('status' => false, 'msg' => '原密码不正确');
            }
            $updateInfo['login_pwd'] = md5($info['pwd']);
            $this->memberInfo['login_pwd'] = md5($info['pwd']);
        } elseif ($info['mark'] == 'operation') {
            # 修改操作密码
            if ($this->memberInfo['operate_pwd'] != md5($info['old_pwd'])) {
                return array('status' => false, 'msg' => '原密码不正确');
            }
            $updateInfo['operate_pwd'] = md5($info['pwd']);
            $this->memberInfo['operate_pwd'] = md5($info['pwd']);
        } else {
            return array('status' => false, 'msg' => '操作标识错误');
        }
        # 判断两次密码是否输入一致
        if ($info['pwd'] != $info['pwd_confirm']) {
            return array('status' => false, 'msg' => '两次密码输入不一致');
        }
        # 修改信息
        $res = Db::table('member')->where($_where)->update($updateInfo);
        if ($res) {
            #修改成功 , 修改redis
            $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
            return array('status' => true, 'msg' => '修改成功');
        } else {
            return array('status' => false, 'msg' => '修改失败');
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @content 首次设置操作密码
     */
    function setOperationPwd()
    {
        $pwd = input('post.pwd');
        $pwd_confirm = input('post.pwd_confirm');
        if (!empty($pwd) and !empty($pwd_confirm)) {
            if ($pwd != $pwd_confirm) {
                return array('status' => false, 'msg' => '两次密码填写不一致');
            } else {
                Db::table('member')->where(array('code' => $this->code))->update(array('operate_pwd' => md5($pwd)));
                $redis = new Redis();
                $this->memberInfo['operate_pwd'] = md5($pwd);
                $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
                return array('status' => true);
            }
        } else {
            return array('status' => false, 'msg' => '参数存在空值');
        }
    }

    /**
     * @return array
     * @content 判断验证码是否填写正确
     */
    function verificationCode()
    {
        $info = input('post.');
        if (!empty($info['phone']) and !empty($info['code'])) {
            $redis = new Redis();
            # 验证码比对
            $redisCode = $redis->hGet('smsCode', strval($info['phone']));
            if (empty($redisCode) or $redisCode != $info['code']) {
                return array('status' => false, 'msg' => '验证码错误');
            } else {
                return array('status' => true, 'msg' => '验证码填写正确');
            }
        } else {
            return array('status' => false, 'msg' => '手机号/验证码为空');
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @content 微信/支付宝/银联  信息修改
     */
    function collectionInfo()
    {
        # where 条件
        $_where = "member_code = '" . $this->code . "'";
        # 修改信息
        $updateInfo = array();
        # 收款码信息
        $imgUrl = '';
        # 接收修改的信息
        $info = input('post.');
        if (!empty($info['mark'])) {
            $redis = new Redis();
            # 上传图片
            if (!empty($info['img'])) {
                # 微信/支付宝  收款码
                $imgUrl = ResourceService::uploadBase($info['img']);
            }

            # 判断参数是否为空
            if ($info['mark'] == 'wx') {
                $updateInfo['wx_img'] = $imgUrl;
                $updateInfo['wx_number'] = $info['account'];
                if (!empty($imgUrl)) {
                    $_delSrc = ROOT_PATH . DS . 'upload' . DS . $this->memberInfo['wx_img'];
                    ResourceService::delFile($_delSrc);
                }
                # 更新redis
                $this->memberInfo['wx_img'] = imgUrl($imgUrl);
                $this->memberInfo['wx_number'] = $info['account'];
            } elseif ($info['mark'] == 'ali') {
                $updateInfo['ali_img'] = $imgUrl;
                $updateInfo['ali_number'] = $info['account'];
                if (!empty($imgUrl)) {
                    $_delSrc = ROOT_PATH . DS . 'upload' . DS . $this->memberInfo['ali_img'];
                    ResourceService::delFile($_delSrc);
                }
                # 更新redis
                $this->memberInfo['ali_img'] = imgUrl($imgUrl);
                $this->memberInfo['ali_number'] = $info['account'];
            } elseif ($info['mark'] == 'unionPay') {
                $updateInfo['union_img'] = $imgUrl;
                $updateInfo['unionpay_number'] = $info['account'];
                $updateInfo['bank'] = $info['bank'];
                $updateInfo['bank_people'] = $info['bank_people'];
                if (!empty($imgUrl)) {
                    $_delSrc = ROOT_PATH . DS . 'upload' . DS . $this->memberInfo['union_img'];
                    ResourceService::delFile($_delSrc);
                }
                $ocr = new OcrService();
                $ocrRes = $ocr->OcrBankCard($imgUrl);
                if ($ocrRes['status']) {
                    if ($ocrRes['CardNumber'] != $info['account']) {
                        return array('status' => false, 'msg' => '银行卡账号填写有误');
                    }
                } else {
                    return $ocrRes;
                }
                # 更新redis
                $this->memberInfo['union_img'] = imgUrl($imgUrl);
                $this->memberInfo['unionpay_number'] = $info['account'];
                $this->memberInfo['bank'] = $info['bank'];
                $this->memberInfo['bank_people'] = $info['bank_people'];
            } else {
                return array('status' => false, 'msg' => '判断标识错误');
            }
            # 进行修改
            $res = Db::table('member_collection')->where($_where)->update($updateInfo);
            if ($res) {
                # 修改成功,更新redis
                $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
                return array('status' => true, 'msg' => '修改成功', 'imgUrl' => imgUrl($imgUrl));
            } else {
                return array('status' => false, 'msg' => '修改失败');
            }
        } else {
            # 存在判断标识
            return array('status' => false, 'msg' => '缺少判断标识');
        }
    }


    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 修改手机号
     */
    function changeMobile()
    {
        $phone = input('post.mobile');
        $code = input('post.code');
        $redis = new Redis();
        $redisCode = $redis->hGet('smsCode', strval($phone));
        if ($code != $redisCode) {
            # 验证码错误
            return array('status' => false, 'msg' => '验证码填写错误');
        }
        # 判断手机号是否已经使用
        $register = MemberService::checkMemberRegister($phone, 'phone');
        if ($register['status']) {
            # 手机号已被注册
            return array('status' => false, 'msg' => '手机号已被注册,请更换手机号');
        }
        # 进行修改
        $res = Db::table('member')->where(array('code' => $this->code))->update(array('phone' => $phone));
        if ($res) {
            # 修改成功,更新redis
            $this->memberInfo['phone'] = $phone;
            $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
            return array('status' => true, 'msg' => '修改成功');
        } else {
            # 修改失败
            return array('status' => false, 'msg' => '修改失败');
        }
    }

    /**
     * @return array
     * @content 意见反馈
     */
    function feedback()
    {
        $info = input('post.');
        if (!empty($info['context']) and !empty($info['mobile'])) {
            $info['member_code'] = $this->code;
            $info['name'] = $this->memberInfo['name'];
            # 保存图片
            if (!empty($info['picurl'])) {
                # 上传图片
                $picurl = '';
//                $info['picurl'] = explode('@', $info['picurl']);
                for ($i = 0; $i < count($info['picurl']); $i++) {
                    if (empty($picurl)) {
                        $picurl .= ResourceService::uploadBase($info['picurl'][$i]);
                    } else {
                        $picurl .= ',' . ResourceService::uploadBase($info['picurl'][$i]);
                    }
                }
                $info['picurl'] = $picurl;
            }
            $info['create_time'] = date("Y-m-d H:i:s");
            Db::table('feedback')->insert($info);
            return array('status' => true, 'msg' => '反馈成功');
        } else {
            return array('status' => false, 'msg' => '参数为空');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content  申请成为合伙人
     */
    function partnerApply()
    {
        $info = input('post.');
        if (!empty($info['p_code'])) {
            # 判断推荐人是否存在
            $isExit = Db::table('member')->field('id')->where(array('code' => strtoupper($info['p_code'])))->find()['id'];
            if (empty($isExit)) {
                return array('status' => false, 'msg' => '推荐人不存在');
            }
        }
        $info_insert['name'] = $this->memberInfo['name'];
        $info_insert['member_code'] = $this->code;
        $info_insert['now_status'] = $this->memberInfo['status'];
        $info_insert['phone'] = $this->memberInfo['phone'];
        $info_insert['memberp_code'] = strtoupper($info['p_code']);
        $info_insert['address'] = $info['address'];
        $info_insert['create_time'] = date("Y-m-d H:i:s");
        $info_insert['member_level'] = $this->memberInfo['level'];
        Db::table('partner_audit')->insert($info_insert);
        return array('status' => true, 'msg' => '申请成功');
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 打劫记录(我打劫的/打劫我的)
     */
    function logRob()
    {
        # 接收查询类型
        $type = input('post.type');
        if ($type == 'robbingMe') {
            # 打劫我的
            $where = "robbed_code = '" . $this->code . "'";
            $leftOn = "lr.member_code = m.code";
        } else {
            # 我打劫的
            $where = "member_code = '" . $this->code . "'";
            $leftOn = "lr.robbed_code = m.code";
        }
        $info = Db::table('log_rob lr')
            ->field("m.name,m.head_portrait,(select  COALESCE(sum(earnings_price),0) from member_business mb where mb.member_code = m.code and mb.status != -1 and place_status=1) power,create_time,lr.status,gold_num,integral_num,member_code,robbed_code")
            ->leftJoin('member m', $leftOn)
            ->where($where)
            ->order('create_time desc')
            ->select();
        if (!empty($info)) {
            foreach ($info as $k => $v) {
                $info[$k]['head_portrait'] = imgUrl($v['head_portrait']);
                $info[$k]['power'] = priceFormat($v['power']);
                $info[$k]['time'] = date('Y/m/d H:i', strtotime($v['create_time']));
            }
        }
        return array('status' => true, 'msg' => '查询成功', 'info' => $info);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 玩家收益记录列表
     */
    function logEarnList()
    {
        # 查询收益记录列表
        $where = "type != -1 and member_code = '" . $this->code . "'";
        # 类型判断
        $type = input('post.type');
        if (!empty($type)) {
            if ($type == 'gold') {
                $where .= " and type in (1,2,5)";
            } else {
                $where .= " and type in (3,4)";
            }
        } else {
            # 默认查询 , 金币收益
            $where .= " and type in (1,2,5)";
        }
        $page = input('post.page') * 10;
        if (empty($page)) {
            $page = 0;
        }
        $info = Db::table('log_earn')->field('id,create_time,type,income')
            ->where($where)
            ->order('create_time desc')
            ->limit($page, 10)->select();
        if (!empty($info)) {
            foreach ($info as $k => $v) {
                $info[$k]['date'] = date('Y/m/d', strtotime($v['create_time']));
                $info[$k]['time'] = date('H:i', strtotime($v['create_time']));
                $info[$k]['typeTitle'] = getLogEranType($v['type']);
                $info[$k]['income'] = priceFormat($v['income']);
            }
        }
        return array('status' => true, 'msg' => '查询成功', 'info' => $info);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 玩家收益记录统计
     */
    function logEarn()
    {
        # 玩家收益统计(1推荐玩家(金币收益)2联盟推荐(金币收益)3经营收益(经营收益)4市场收益(经营收益)5平台奖励(金币收益))
        $member_code = input('post.member_code');
        if (!empty($member_code)) {
            $where = "log_earn.type != -1 and member_code = '" . $member_code . "'";
        } else {
            $where = "log_earn.type != -1 and member_code = '" . $this->code . "'";
        }
        $where_time = '';
        #日期筛选
        $dateType = input('post.dateType');
        if (!empty($dateType)) {
            if ($dateType == 'day') {
                # 今日
                $where_time = " and date(create_time) = '" . date('Y-m-d') . "'";
                $_time = date('Y-m-d');
            } elseif ($dateType == 'week') {
                # 本周
                $begin = beginWeek(false);
                $end = endWeek(false);
                $where_time = " and date(create_time) >= '" . $begin . "' and date(create_time) <= '" . $end . "'";
                $_time = $begin . '~' . $end;
            } elseif ($dateType == 'month') {
                #本月
                $where_time = " and date_format(create_time,'%Y-%m') = '" . date('Y-m') . "'";
                $_time = date('Y-m-01') . '~' . date('Y-m-d');
            }
        } else {
            $where_time = " and date(create_time) = '" . date('Y-m-d') . "'";
            $_time = date('Y-m-d');
        }
        # 日期搜索
        $start_time = input('post.start_time');
        $end_time = input('post.end_time');
        if (!empty($start_time) or !empty($end_time)) {
            $where_time = '';
            $_time = '';
            if (!empty($start_time)) {
                $where_time .= " and date(create_time) >= '" . $start_time . "'";
                $_time .= $start_time;
            }
            if (!empty($end_time)) {
                $where_time .= " and date(create_time) <= '" . $end_time . "'";
                $_time .= $end_time;
            }
        }

        # 收益类型统计
        $incomeType = Db::table('log_earn')
            ->field("convert(coalesce(sum(income),0),decimal(10,2)) income,typeBranch.type")
            ->join('(select 1 as type union 
            select 2 as type union select 3 as type union 
            select 4 as type union select 5 as type) as typeBranch', $where . $where_time . 'and typeBranch.type=log_earn.type', 'right')
            ->group('typeBranch.type')
            ->order('typeBranch.type asc')
//            ->where($where . $where_time)
            ->select();
        $incomeDataCount = array_sum(array_column($incomeType, 'income'));
        if (!empty($incomeType)) {
            foreach ($incomeType as $k => $v) {
                $incomeType[$k]['typeTitle'] = getLogEranType($v['type']);
                if (empty($incomeDataCount)) {
                    $incomeType[$k]['proportion'] = 0;
                } else {
                    $incomeType[$k]['proportion'] = floatval(priceFormat($v['income'] / $incomeDataCount * 100));
                }
                if ($v['type'] == 1) {
                    $incomeType[$k]['color'] = 'green';
                } elseif ($v['type'] == 2) {
                    $incomeType[$k]['color'] = 'orange';
                } elseif ($v['type'] == 3) {
                    $incomeType[$k]['color'] = 'yellow';
                } elseif ($v['type'] == 4) {
                    $incomeType[$k]['color'] = 'blue';
                } elseif ($v['type'] == 5) {
                    $incomeType[$k]['color'] = 'purple';
                } else {
                    $incomeType[$k]['color'] = 'blue';
                }
            }
            $proportion = array_column($incomeType, 'proportion');
        }
        return array('status' => true, 'msg' => '查询成功',
            'data' => array('incomeData' => $incomeType, 'proportion' => $proportion, 'incomeDataCount' => priceFormat($incomeDataCount)),
            'income' => priceFormat($this->memberInfo['income']), 'time' => $_time);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 交易记录
     */
    function logTrad()
    {
        # 查询交易记录
        $_where = "(seller_code = '" . $this->code . "' or buyer_code = '" . $this->code . "')";
        # 默认时间倒序
        $order = 'buy_time desc';
        # 接收排序条件
        $info = input('post.');
        # 时间排序(asc:正序  desc 倒序)
        if (!empty($info['time'])) {
            # 时间排序
            $order = "lt.buy_time " . $info['time'];
        }
        # 价值排序(asc:正序 desc:倒序)
        if (!empty($info['price'])) {
            # 价值排序
            $order = "buy_price " . $info['price'];
        }
        # 筛选
        if (!empty($info['type'])) {
            # 购进
            if ($info['type'] == 1) {
                $_where = "(buyer_code = '" . $this->code . "')";
            }
            # 出售
            if ($info['type'] == 2) {
                $_where = "(seller_code = '" . $this->code . "')";
            }
        }
//        $field = "*,if(buyer_code='" . $this->code . "',1,2) status,date_format(buy_time,'%Y/%m/%d %H:%i') buyTime,date_format(sell_time,'%Y/%m/%d %H:%i') sellTime";
//        $_return = MemberService::logInfo('log_trad', $field, $_where, $order, false);
        $_return = Db::table('log_trad lt')
            ->join('member_business mb', "if(buyer_code='" . $this->code . "',mb.id=lt.buyer_business_id,mb.id=lt.member_business_id)", 'right')
            ->field("lt.*,(lt.buy_price+lt.voucher_price) buy_price,mb.status mbStatus,date_format(mb.trade_finish_time ,'%Y/%m/%d %H:%i') finishTime,if(buyer_code='" . $this->code . "',1,2) status,date_format(mb.buy_time,'%Y/%m/%d %H:%i') buyTime,date_format(mb.sell_time,'%Y/%m/%d %H:%i') sellTime")
            ->where($_where)
            ->order($order)
            ->select();
        return array('status' => true, 'msg' => '查询成功', 'info' => $_return);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @content 兑换恢复材料
     */
    function exchangeMaterials()
    {
        $info = input('post.');
        if (!empty($info['num']) and !empty($info['pwd'])) {
            $redis = new Redis();
            # 判断金币数量是否足够(2个金币兑换一个恢复材料)
            $goldNum = $info['num'] * 2;
            if ($goldNum > $this->memberInfo['gold']) {
                # 金币数量不够
                return array('status' => false, 'msg' => '金币数量不足');
            }
            # 判断操作密码是否正确
            if (md5($info['pwd']) != $this->memberInfo['operate_pwd']) {
                return array('status' => false, 'msg' => '操作密码不正确');
            }
            # 兑换(扣除金币,增加恢复材料)
            $gold_return = Db::table('member_property')->where(array('member_code' => $this->code))->setDec('gold', $goldNum);
            if ($gold_return) {
                # 扣除成功 ,添加金币变化记录, 增加恢复材料
                MemberService::addLog_gold($this->code, $goldNum, 10);
                $materials_return = Db::table('member_property')->where(array('member_code' => $this->code))->setInc('materials', $info['num']);
                if ($materials_return) {
                    # 增加记录
                    MemberService::addLog_materials($this->code, $info['num'], 1, 1, 0);
                    # 修改redis
                    $this->memberInfo['materials'] = priceFormat($this->memberInfo['materials'] + $info['num']);
                    $this->memberInfo['gold'] = priceFormat($this->memberInfo['gold'] - $goldNum);
                    $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
                    # 完成任务=>兑换材料
                    TaskService::finishTask($this->code, 10);
                    return array('status' => true, 'msg' => '兑换成功');
                } else {
                    return array('status' => false, 'msg' => '恢复材料兑换失败');
                }
            } else {
                return array('status' => false, 'msg' => '金币扣除失败');
            }
        } else {
            return array('status' => false, 'msg' => '参数存在空值');
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @content 金币/积分 转账
     */
    function transfer()
    {
        $info = input('post.');
        if (!empty($info['num']) and !empty($info['pwd']) and !empty('p_code') and !empty($info['mark'])) {
            # 判断是否已经实名制
            if ($this->memberInfo['realname_status'] == 1) {
                return array('status' => false, 'msg' => '请先去实名制');
            }
            # 判断是否达到第三等级
            if ($this->memberInfo['level'] < 3) {
                $levelTitle = Db::table('member_level')->field('title')->where(array('level' => 3))->find()['title'];
                return array('status' => false, 'msg' => '当前等级不满足转账条件！请达到' . $levelTitle . '后再试！');
            }
            $redis = new Redis();
            $pMemberInfo = $redis->hGetJson('memberInfo', strtoupper($info['p_code']));
            # 获取转账玩家的信息(判断余量是否足够)(加上手续费)(最少10个 , 最多100个)
            $serviceCharge = $info['num'] * 0.1;
            if ($serviceCharge < 10) {
                $serviceCharge = 10;
            }
            if ($serviceCharge > 100) {
                $serviceCharge = 100;
            }
            $num = $info['num'] + $serviceCharge;
            if ($info['mark'] == 'gold') {
                # 判断金币数量是否足够
                if ($num > $this->memberInfo['gold']) {
                    # 金币数量不够
                    return array('status' => false, 'msg' => '金币数量不足');
                }
            } elseif ($info['mark'] == 'integral') {
                # 判断 恢复材料是否足够
                if ($num > $this->memberInfo['integral']) {
                    # 金币数量不够
                    return array('status' => false, 'msg' => '积分不足');
                }
            }
            # 判断操作密码是否正确
            if (md5($info['pwd']) != $this->memberInfo['operate_pwd']) {
                return array('status' => false, 'msg' => '操作密码不正确');
            }
            # 进行转账
            if ($info['mark'] == 'gold') {
                $itype = '金币';
                # 扣除金币
                $deduction = Db::table('member_property')->where(array('member_code' => $this->code))->setDec('gold', $num);
                if ($deduction) {
                    # 添加金币变化记录
                    MemberService::addLog_gold($this->code, $num, 1, strtoupper($info['p_code']));
                    # 修改redis
                    $this->memberInfo['gold'] = priceFormat($this->memberInfo['gold'] - $num);
                    # 添加金币
                    $deductions = Db::table('member_property')->where(array('member_code' => strtoupper($info['p_code'])))->setInc('gold', $info['num']);
                    if ($deductions) {
                        # 修改redis
                        $pMemberInfo['gold'] = priceFormat($pMemberInfo['gold'] + $info['num']);
                        # 添加金币变化记录
                        MemberService::addLog_gold(strtoupper($info['p_code']), $info['num'], 2, $this->code);
                    }
                    # 完成任务=>金币转账
                    TaskService::finishTask($this->code, 4);
                    # 判断是否发布过交易大厅信息并删除当天信息
                    $trading_info = Db::table("trading_room")->field("id")->where("member_code='" . $this->code . "'")->order("id desc")->find();
                    if (!empty($trading_info)) {
                        Db::table("trading_room")->where(array("id" => $trading_info['id']))->delete();
                    }
                }
            } elseif ($info['mark'] == 'integral') {
                $itype = '积分';
                # 扣除积分
                $deduction = Db::table('member_property')->where(array('member_code' => $this->code))->setDec('integral', $num);
                if ($deduction) {
                    # 修改redis
                    $this->memberInfo['integral'] = priceFormat($this->memberInfo['integral'] - $num);
                    # 添加积分变化记录
                    MemberService::addLog_integral($this->code, $num, 2, 7, strtoupper($info['p_code']));
                    # 添加积分
                    $deductions = Db::table('member_property')->where(array('member_code' => strtoupper($info['p_code'])))->setInc('integral', $info['num']);
                    if ($deductions) {
                        # 修改redis
                        $pMemberInfo['integral'] = priceFormat($pMemberInfo['integral'] + $info['num']);
                        # 添加积分变化记录
                        MemberService::addLog_integral(strtoupper($info['p_code']), $info['num'], 1, 8, $this->code);
                    }
                    # 完成任务=>积分转账
                    TaskService::finishTask($this->code, 11);
                }
            }
            if ($deduction) {
                # 更新redis
                $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
                $redis->hSetJson('memberInfo', $info['p_code'], $pMemberInfo);
                # 消息推送
                $IGeTui = new IGeTuiService();
                $IGeTui->pushMessageToSingle('转账', '收到一笔转账', $this->memberInfo['name'] . '已成功向你转了' . $info['num'] . $itype, $info['p_code'], 2);
                return array('status' => true, 'msg' => '转账成功');
            } else {
                return array('status' => false, 'msg' => '转账失败');
            }
        } else {
            return array('status' => false, 'msg' => '参数存在空值');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取用户信息
     */
    function memberInfo()
    {
        # 查询玩家信息()
        $_searchInfo = input('post.searchInfo');
        $mark = input('post.mark');
        if (!empty($_searchInfo)) {
            # 通过手机号,玩家ID 查询
            if (CheckMobile($_searchInfo)) {
                # 手机号
                $_return = MemberService::checkMemberRegister($_searchInfo, 'phone');
            } else {
                # 玩家ID
                $redis = new Redis();
                $_return = $redis->hGetJson('memberInfo', strtoupper($_searchInfo));
                $_returnMark = false;
                if (empty($_return)) {
                    $_returnMark = true;
                    $_return = MemberService::checkMemberRegister($_searchInfo, 'account');
                }
//                dump($_returnMark);die;
                if (!$_returnMark) {
                    $info = $_return;
                    $_return = array();
                    $_return['status'] = true;
                    $_return['info'] = $info;
                }
            }
            if ($mark == 'transfer') {
                if ($_return['status']) {
                    $_return['info'] = array(
                        'code' => $_return['info']['code'],
                        'name' => $_return['info']['name'],
                        'phone' => hidPhone($_return['info']['phone'], 3, 4),
                        'phoneInfo' => $_return['info']['phone'],
                        'title' => $_return['info']['title'],
                        'head_portrait' => imgUrl($_return['info']['head_portrait'])
                    );
                }
            }
            if ($mark == 'tradingReport') {
                if ($_return['status']) {
                    $_return['info'] = array(
                        'code' => $_return['info']['code'],
                        'name' => $_return['info']['name'],
                        'hidphone' => hidPhone($_return['info']['phone'], 3, 4),
                        'phone' => $_return['info']['phone'],
                        'level_title' => $_return['info']['title'],
                        'headimg' => imgUrl($_return['info']['head_portrait'])
                    );
                }
            }
            if ($mark == 'playerDetail') {
                # 玩家详情页面使用
                # 查询是否是好友
                $friends = Db::table('friends')->field('relation_ship')
                    ->where(array('member_code' => $this->code, 'friends_code' => $_searchInfo, 'status' => 2))
                    ->find();
                if (!empty($friends)) {
                    $_return['info']['friends'] = $friends['relation_ship'];
                } else {
                    $_return['info']['friends'] = 0;
                }
                $_return['info']['friendsType'] = getFriendsRelationShip($friends['relation_ship']);
                # 是否是活跃用户
                $activeState = Db::table('member_business_grid')
                    ->where("member_code = '" . $_searchInfo . "' and status = 1 and member_business_id=0 
                and start_time < '" . date('Y-m-d H:i:s', strtotime('- 24 hours')) . "'")
                    ->count();
                if ($activeState > 0) {
                    # 不活跃
                    $_return['info']['activeState'] = 0;
                } else {
                    # 活跃
                    $_return['info']['activeState'] = 1;
                }
                # 查询团队人数
                $_return['info']['team_num'] = Db::table('member')->where(array('p_code' => $_searchInfo))->where(array('status' => 5, 'fictitious' => 0))->count();
            }
//            dump($_return);die;
            if ($mark == 'addFriends') {
                # 查询对方是否是我的好友
                $friends = Db::table('friends')->field('relation_ship,if(note_name="" or note_name is null,nickname,note_name) note_name')
                    ->where(array('member_code' => $this->code, 'friends_code' => $_return['info']['code'], 'status' => 2))
                    ->find();
                if (!empty($friends)) {
                    # 判断我是否是对方的好友
                    $friendState = Db::table('friends')->field('relation_ship,if(note_name="" or note_name is null,nickname,note_name) note_name')
                        ->where(array('member_code' => $_return['info']['code'], 'friends_code' => $this->code, 'status' => 2))
                        ->find();
                    if (!empty($friendState)) {
                        $_return['info']['friends'] = $friends['relation_ship'];
                        $_return['info']['relation_ship'] = $friends['relation_ship'];
                        $_return['info']['note_name'] = $friends['note_name'];
                        if (!empty($friends['note_name'])) {
                            $_return['info']['name'] = $friends['note_name'];
                        }
                    } else {
                        goto notFriend;
                    }
                } else {
                    notFriend:
                    $_return['info']['friends'] = 0;
                    $_return['info']['note_name'] = "";
                    # 判断好友关系
                    if ($this->memberInfo['p_code'] == $_return['info']['code']) {
                        # 我的推荐人
                        $_return['info']['relation_ship'] = 1;
                    } elseif ($_return['info']['p_code'] == $this->code) {
                        # 我的直推
                        $_return['info']['relation_ship'] = 2;
                    } else {
                        # 判断是我的团队还是好友
                        $p_code = Db::table('member')->field('p_code')->where(array('p_code' => $_return['info']['p_code']))->find()['p_code'];
                        if ($this->code == $p_code) {
                            # 我的团队
                            $_return['info']['relation_ship'] = 3;
                        } else {
                            # 我的好友
                            $_return['info']['relation_ship'] = 4;
                        }
                    }
                }
            }
            return $_return;
        } else {
            # 通过header头中的参数  查询
            # 查询可用的卡券数量
            $voucherCount = Db::table('member_voucher')
                ->where("member_code = '" . $this->code . "' and use_status = 1 and expiration_time > '" . date("Y-m-d H:i:s") . "'")
                ->count();
            $this->memberInfo['voucherCount'] = $voucherCount;
            return array('status' => true, 'msg' => '查询成功', 'info' => $this->memberInfo);
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 卡券兑换1.2倍金币
     */
    function exchangeVoucher()
    {
        $voucherId = input('post.id');
        $voucherPrice = input('post.price');
        if (!empty($voucherId) and !empty($voucherPrice)) {
            # 查询状态
            $status = Db::table('member_voucher')->field('id')
                ->where("id = " . $voucherId . " and use_status = 1 and expiration_time > '" . date("Y-m-d H:i:s") . "'")->find();
            if (!empty($status)) {
                $gold = $voucherPrice * 1.2;
                # 修改卡券状态
                $res = Db::table('member_voucher')->where(array('id' => $voucherId))->update(array('use_status' => 2, 'use_time' => date("Y-m-d H:i:s")));
                if ($res) {
                    # 增加金币
                    Db::table('member_property')->where(array('member_code' => $this->code))->setInc('gold', $gold);
                    Db::table('member_property')->where(array('member_code' => $this->code))->setInc('income', $gold);
                    # 金币记录
                    MemberService::addLog_gold($this->code, $gold, 15);
                    # 添加收益记录
                    MemberService::addLog_income($this->code, $gold, 5, 10);
                    # 更新redis
                    $redis = new Redis();
                    $this->memberInfo['gold'] = priceFormat($this->memberInfo['gold'] + $gold);
                    $this->memberInfo['income'] = priceFormat($this->memberInfo['income'] + $gold);
                    $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
                    return array('status' => true, 'msg' => '兑换成功');
                } else {
                    return array('status' => false, 'msg' => '卡券兑换失败');
                }
            } else {
                return array('status' => false, 'msg' => '无法兑换,该卡券已过期/已使用');
            }
        } else {
            return array('status' => false, 'msg' => '参数存在空值');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 用户卡券
     */
    function memberVoucher()
    {
        # 定义where 条件
        $where = "member_code = '" . $this->code . "'";
        # 接收查询参数
        $mark = input('post.mark');
        # 对参数进行判断(默认为 未使用的)
        if (empty($mark)) {
            # 默认
            $where .= " and use_status = 1 and expiration_time > '" . date("Y-m-d H:i:s") . "'";
        } else {
            if ($mark == 'notUsed') {
                # 未使用的
                $where .= " and use_status = 1 and expiration_time > '" . date("Y-m-d H:i:s") . "'";
            } else if ($mark == 'used') {
                $where .= " and use_status = 2";
            } else if ($mark == 'expired') {
                $where .= " and use_status = 1 and expiration_time <= '" . date("Y-m-d H:i:s") . "'";
            }
        }
        # 查询用户卡券
        $info = Db::table('member_voucher')
            ->field('id,voucher_title,price,date(expiration_time) expiration_time,source,source_branch')
            ->where($where)
            ->select();
        if (!empty($info)) {
            foreach ($info as $k => $v) {
                # 卡券来源
                $info[$k]['sourceTitle'] = getVoucherSource($v['source'], $v['source_branch']);
            }
        }
        return array('status' => true, 'msg' => '查询成功', 'info' => $info);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 金币记录
     */
    function memberGold()
    {
        # 查询用户金币记录
        $field = 'gold_num,type,create_time';
        # 定义where条件
        $_where = "member_code = '" . $this->code . "'";
        # 接收搜索参数
        $searchInfo = input('post.');
        $listRows = 10;
        $pageMark = input('post.page');
        if (empty($pageMark)) {
            $pageMark = 0;
        }
//        $page = $pageMark * $listRows;
        $page = 0;
        if (!empty($searchInfo)) {
            if (!empty($searchInfo['type'])) {
                # 类型筛选
                $_where .= " and type = " . $searchInfo['type'];
            }
            if (!empty($searchInfo['start_time'])) {
                $_where .= " and date(create_time) >= '" . $searchInfo['start_time'] . "'";
            }
            if (!empty($searchInfo['end_time'])) {
                $_where .= " and date(create_time) <= '" . $searchInfo['end_time'] . "'";
            }
        }
        $info = MemberService::logInfo('log_gold', $field, $_where, array('id' => 'desc'), false, $listRows, null, $page);
        if (!empty($info)) {
            foreach ($info as $k => $v) {
                $info[$k]['time'] = date('Y-m-d H:i', strtotime($v['create_time']));
                $info[$k]['typeTitle'] = getGoldType($v['type'], $v['type_branch']);
                if (in_array($v['type'], [1, 8, 9, 10, 13, 14, 16, 21, 23, 24, 26])) {
                    $info[$k]['numInfo'] = '-' . priceFormat($v['gold_num']);
                } else {
                    $info[$k]['numInfo'] = '+' . priceFormat($v['gold_num']);
                }
            }
        }
        return array('status' => true, 'msg' => '查询成功', 'info' => $info, 'gold' => priceFormat($this->memberInfo['gold']));
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 用户恢复材料记录
     */
    function memberMaterials()
    {
        # 查询用户恢复材料记录
        $field = 'change_num,type,create_time,status';
        # 定义where条件
        $_where = "member_code = '" . $this->code . "'";
        # 接收搜索参数
        $searchInfo = input('post.');
        $listRows = 10;
        $pageMark = input('post.page');
        if (empty($pageMark)) {
            $pageMark = 0;
        }
        $page = $pageMark * $listRows;
        if (!empty($searchInfo)) {
            if (!empty($searchInfo['type'])) {
                # 类型筛选
                $_where .= " and type = " . $searchInfo['type'];
            }
            if (!empty($searchInfo['start_time'])) {
                $_where .= " and date(create_time) >= '" . $searchInfo['start_time'] . "'";
            }
            if (!empty($searchInfo['end_time'])) {
                $_where .= " and date(create_time) <= '" . $searchInfo['end_time'] . "'";
            }
        }
        $info = MemberService::logInfo('log_materials', $field, $_where, array('id' => 'desc'), false, $listRows, null, $page);
        if (!empty($info)) {
            foreach ($info as $k => $v) {
                $info[$k]['time'] = date('Y-m-d H:i', strtotime($v['create_time']));
                $info[$k]['typeTitle'] = getMaterialsType($v['type']);
                if ($v['status'] == 2) {
                    $info[$k]['numInfo'] = '-' . priceFormat($v['change_num']);
                } elseif ($v['status'] == 1) {
                    $info[$k]['numInfo'] = '+' . priceFormat($v['change_num']);
                } else {
                    $info[$k]['numInfo'] = priceFormat($v['change_num']);
                }
            }
        }
        return array('status' => true, 'msg' => '查询成功', 'info' => $info,
            'materials' => priceFormat($this->memberInfo['materials']), 'gold' => priceFormat($this->memberInfo['gold']),
            'canExchange' => priceFormat($this->memberInfo['gold'] / 2));
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 用户积分记录
     */
    function memberIntegral()
    {
        # 查询用户积分记录
        $field = 'num,type,create_time,status';
        # 定义where条件
        $_where = "member_code = '" . $this->code . "'";
        # 接收搜索参数
        $searchInfo = input('post.');
        $listRows = 10;
        $pageMark = input('post.page');
        if (empty($pageMark)) {
            $pageMark = 0;
        }
        $page = $pageMark * $listRows;
        if (!empty($searchInfo)) {
            if (!empty($searchInfo['type'])) {
                # 类型筛选
                $_where .= " and type = " . $searchInfo['type'];
            }
            if (!empty($searchInfo['start_time'])) {
                $_where .= " and date(create_time) >= '" . $searchInfo['start_time'] . "'";
            }
            if (!empty($searchInfo['end_time'])) {
                $_where .= " and date(create_time) <= '" . $searchInfo['end_time'] . "'";
            }
        }
        $info = MemberService::logInfo('log_integral', $field, $_where, array('id' => 'desc'), false, $listRows, null, $page);
        if (!empty($info)) {
            foreach ($info as $k => $v) {
                $info[$k]['time'] = date('Y-m-d H:i', strtotime($v['create_time']));
                $info[$k]['typeTitle'] = getIntegralType($v['type']);
                if ($v['status'] == 2) {
                    $info[$k]['numInfo'] = '-' . priceFormat($v['num']);
                } elseif ($v['status'] == 1) {
                    $info[$k]['numInfo'] = '+' . priceFormat($v['num']);
                } else {
                    $info[$k]['numInfo'] = priceFormat($v['num']);
                }
            }
        }
        return array('status' => true, 'msg' => '查询成功', 'info' => $info, 'integral' => priceFormat($this->memberInfo['integral']));
    }


    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 更改摇奖弹窗状态
     */
    function changeBigRunner()
    {
        $mark = input("post.mark");
        Db::table("member")->where(array("code" => $this->code))->update(array("bigrunner_tip" => $mark));
        $_redis = new Redis();
        $this->memberInfo['bigrunner_tip'] = $mark;
        $_redis->hSetJson("memberInfo", $this->code, $this->memberInfo);
        return array("status" => true);
    }
}